package com.reggielcy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggielcy.common.R;
import com.reggielcy.entity.User;
import com.reggielcy.service.UserService;
import com.reggielcy.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @auther teacher
 * @date 2024/3/29
 * @project reggie_take_out
 */

/**
 * 移动端
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

        @PostMapping("/sendSms")
        public R<String> sendSms(@RequestBody User user, HttpSession httpSession) {
            if (StringUtils.isNotEmpty(user.getPhone())) {

                String code = ValidateCodeUtils.generateValidateCode4String(4);
                log.info("code={}",code);

                //调用阿里云短信验证服务
                //sendSms(code,...)
                String phone=user.getPhone();

                //存入session
                //httpSession.setAttribute(phone,code);

                //优化:将验证码缓存至redis中 有效期五分钟
                redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
                return R.success("发送短信验证码成功");
            }
            return R.error("手机号不能为空");
        }


        /*//有验证码的接口
        @PostMapping("/login")
        public R<String> login(@RequestBody Map map ,HttpSession httpSession) {
            String phone = (String) map.get("phone");
            String code = (String) map.get("code");

           // String codeInSession =(String) httpSession.getAttribute(phone);

            //优化 从redis中取出验证码
            String codeInSession=(String) redisTemplate.opsForValue().get(phone);

            if (StringUtils.isNotEmpty(phone)&&code.equals(codeInSession)){

                LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(User::getPhone,phone);
                User user = userService.getOne(queryWrapper);
                if (user==null){
                    user=new User();
                    user.setPhone(phone);
                    user.setStatus(1);
                    userService.save(user);
                }
                httpSession.setAttribute("user",user.getId());

                //如果登录成功，则清楚redis中的验证码
                redisTemplate.delete(phone);

                return R.success("登录成功");

            }
            return R.error("登录失败");
        }*/


    //无验证码的简单接口
    @PostMapping("/login")
    public R<String> login(@RequestBody Map map,HttpSession session) {
        String phone = (String) map.get("phone");

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone);
        User user = userService.getOne(queryWrapper);

        if (user == null) {
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }

        session.setAttribute("user",user.getId());
        redisTemplate.delete(phone);
        return R.success("登录成功");

    }


}
